var axios = require("axios");

module.exports = {
    namespaced: true,
    state: {
        // 库存信息
        inventory: {
            allInventory: [],
            inventoryList: [],
            pageSize: 5,
            pageNumber: 1,
            maxNumbers: '',
            updateInventory: {},
        },
        merchantId: "5ecdd5f0933400001e003296",
    },
    mutations: {
        getInventoryList(state, data) {
            state.inventory.inventoryList = [...data.rows];
            state.inventory.maxNumbers = data.maxNumbers;
        },
        getAllInventory(state, data) {
            state.inventory.allInventory = [...data];
        },
        activeUpdateInventory(state, data) {
            state.inventory.updateInventory = { ...data };
        },
        nextPage(state) {
            if (state.inventory.pageNumber < state.inventory.maxNumbers) {
                state.inventory.pageNumber++
            }
        },
        previous(state) {
            if (state.inventory.pageNumber > 1) {
                state.inventory.pageNumber--;
            }
        },
        firstPage(state) {
            state.inventory.pageNumber = 1;
        },
        maxPage(state) {
            state.inventory.pageNumber = state.inventory.maxNumbers;
        },
        inquirePage(state) {
            state.inventory.pageNumber = 1;
        },
        inquireData(state, data) {
            state.inventory.inventoryList = [...data.rows];
            state.inventory.maxNumbers = data.maxNumbers;
        }
    },
    actions: {
        // 获取库存列表
        getInventoryListAsync(context, data) {
            console.log('获取库存',data)
            axios.get("/merchant/getInventoryList", { params: { merchantId: data, pageSize: context.state.inventory.pageSize, pageNumber: context.state.inventory.pageNumber } }).then(({ data }) => {
                context.commit("getInventoryList", data)
            })
        },
        getAllInventoryAsync(context, data) {
            // console.log(context.state.inventory.pageSize)
            axios.get("/merchant/getAllInventory", { params: { data } }).then(({ data }) => {
                context.commit("getAllInventory", data)
            })
        },
        // 获取要修改的数据
        activeUpdateInventoryAsync(context, data) {
            axios.get("/merchant/getInventoryById", { params: { _id: data._id } }).then(({ data }) => {
                console.log(data)
                context.commit("activeUpdateInventory", { ...data })
            })

        },
        updateInventoryAsync(context, data) {
            axios.post("/merchant/updateInventory", { ...data }).then(({ data }) => {
                console.log(data);
            })
        },
        addInventoryAsync(context, data) {
            // console.log(data)
            return axios.post("/merchant/addInventory", { ...data }).then(({ data }) => {
                console.log(data);
                context.dispatch("getInventoryListAsync");
                return data;
            });
        },
        removeInventoryAsync(context, data) {
            console.log(data)
            axios.get("/merchant/removeInventory", { params: { _id: data._id, img: data.img, particularsImg: data.particularsImg } }).then(({ data }) => {
                context.dispatch("getInventoryListAsync");
            });
        },
        // 下一页
        nextPageAsync(context) {
            context.commit('nextPage');
        },
        // 上一页
        previousAsync(context) {
            context.commit('previous');
        },
        // 首页
        firstPageAsync(context) {
            context.commit('firstPage');
        },
        // 尾页
        maxPageAsync(context) {
            context.commit('maxPage');
        },
        // 库存搜索
        quireInventoryAsync(context, data) {
            // console.log(data)
            context.commit("inquirePage");
            axios.post("/merchant/quireInventory", { pageSize: context.state.inventory.pageSize, pageNumber: context.state.inventory.pageNumber, ...data }).then(({ data }) => {
                // console.log(data)
                context.commit("inquireData", data);
            })
        },
        //上架商品
        async putaway(context, data1) {
            console.log('上架',data1)
            var { data } = await axios.post('/merchant/addcommodity', { data1 })
            return data
        }
    },
}